/************************************************************************************/
/*  Program name CAPFAIMP_ASSET_INV.sql                                        		*/
/*  Creater:  NIP Karen (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 24 JAN, 2006   NIP Karen         First created                               	*/
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFAIMP_ASSET_INV';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	/*SELECT
		*
	FROM
		CRST_FA_ASSET_INVOICES;*/
	SELECT
		BOOK_TYPE_CODE, C1.*
	FROM
		CRST_FA_ASSET_INVOICES C1,
		FA_BOOKS X
		WHERE X.ASSET_ID = CAPBRIMP_MAPPING.GetAssetIDMapping(C1.ASSET_ID) ;
	
	lv_Next_Seq_ID		NUMBER;
	lv_Asset_ID			NUMBER;
	lv_Book_Type_Code	VARCHAR2(100);
	lv_COA_ID			NUMBER;

BEGIN
	FOR C1 IN Cur_F LOOP
		BEGIN
			SELECT	FA_ASSET_INVOICES_S.NextVal
			INTO	lv_Next_Seq_ID
			FROM	DUAL;

			INSERT	INTO CRST_FA_ASSET_INVOICES_MAP	VALUES
			(
			C1.SOURCE_LINE_ID,   
			lv_Next_Seq_ID 
			);
			
			lv_Asset_ID	:=	CAPBRIMP_MAPPING.GetAssetIDMapping(C1.ASSET_ID);
			
			/*SELECT	BOOK_TYPE_CODE
			INTO	lv_Book_Type_Code
			FROM	FA_BOOKS
			WHERE	ASSET_ID	=	lv_Asset_ID;
			AND		DATE_EFFECTIVE IS NULL;*/
			
			lv_COA_ID	:=	CAPBRIMP_COMMON.GetCOAIDfromBTC(c1.Book_Type_Code);

			INSERT INTO FA_ASSET_INVOICES
			VALUES
			(
			lv_Asset_ID,
			C1.PO_VENDOR_ID,
			C1.ASSET_INVOICE_ID,
			C1.FIXED_ASSETS_COST,
			C1.DATE_EFFECTIVE,
			C1.DATE_INEFFECTIVE,
			CAPBRIMP_MAPPING.GetFAInvoiceTXNIDMapping(C1.INVOICE_TRANSACTION_ID_IN),
			CAPBRIMP_MAPPING.GetFAInvoiceTXNIDMapping(C1.INVOICE_TRANSACTION_ID_OUT),
			C1.DELETED_FLAG,
			C1.PO_NUMBER,
			C1.INVOICE_NUMBER,
			C1.PAYABLES_BATCH_NAME,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.PAYABLES_CODE_COMBINATION_ID_D),
			C1.FEEDER_SYSTEM_NAME,
			C1.CREATE_BATCH_DATE,
			C1.CREATE_BATCH_ID,
			C1.INVOICE_DATE,
			C1.PAYABLES_COST,
			C1.POST_BATCH_ID,
			CAPBRIMP_MAPPING.GetAPInvoiceIDMapping(C1.INVOICE_ID),
			C1.AP_DISTRIBUTION_LINE_NUMBER,
			C1.PAYABLES_UNITS,
			C1.SPLIT_MERGED_CODE,
			C1.DESCRIPTION,
			C1.PARENT_MASS_ADDITION_ID ,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_NAME),
			CAPBRIMP_COMMON.GetUser(C1.CREATED_NAME),
			C1.CREATION_DATE,
			C1.LAST_UPDATE_LOGIN,
			C1.ATTRIBUTE1,
			C1.ATTRIBUTE2,
			C1.ATTRIBUTE3,
			C1.ATTRIBUTE4,
			C1.ATTRIBUTE5,
			C1.ATTRIBUTE6,
			C1.ATTRIBUTE7,
			C1.ATTRIBUTE8,
			C1.ATTRIBUTE9,
			C1.ATTRIBUTE10,
			C1.ATTRIBUTE11,
			C1.ATTRIBUTE12,
			C1.ATTRIBUTE13,
			C1.ATTRIBUTE14,
			C1.ATTRIBUTE15,
			C1.ATTRIBUTE_CATEGORY_CODE,
			C1.UNREVALUED_COST,
			C1.MERGED_CODE,
			C1.SPLIT_CODE,
			C1.MERGE_PARENT_MASS_ADDITIONS_ID,
			C1.SPLIT_PARENT_MASS_ADDITIONS_ID,
			C1.PROJECT_ASSET_LINE_ID,
			C1.PROJECT_ID,
			C1.TASK_ID,
			lv_Next_Seq_ID
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/